global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_2_alm_sectors.log, replace name(tabl)

capture noi {

    * v2
    * List sectors with highest and lowest shares of automation patents in machinery

    * ---------------------------------
    * Shares
    * ---------------------------------

    texdoc init ${tab_dir}/main/Table_2_alm_sectors.tex, replace
    texdoc stlog, nolog

    global cvers _ipc4
    use ${final_dir}/ind6090_patents_regression_data${cvers}, clear

    * highest share of automated patents
    gsort -sh_auto95_use ind6090
    list ind6090 sh_auto95_use in f/6, noobs sep(6) table abb(25)
    forval i = 1/6 { 
        texdoc local h`i'_id : display %9.0f ind6090[`i']
        sleep 2000
        texdoc local h`i'_a95 : display %9.3f sh_auto95_use[`i']
        sleep 2000
    }
    *please note that while we hardcode the descriptions here, the codes are dynamic. If rerunning changes the codes, the description will be empty (should obviously not happen).
    texdoc local 756_descr "Automotive services and repair shops"
    texdoc local 206_descr "Household appliances (e.g., radio, TV, equipment)"
    texdoc local 470_descr "Water supply and irrigation"
    texdoc local 271_descr "Iron and steal foundaries"
    texdoc local 130_descr "Tobacco manufactures"
    texdoc local 212_descr "Misc. plastic products"

    * lowest share of automated patents
    sort sh_auto95_use ind6090
    list ind6090 sh_auto95_use in f/6, noobs sep(6) table abb(25)
    forval i = 1/6 { 
        texdoc local l`i'_id : display %9.0f ind6090[`i']
        sleep 2000
        texdoc local l`i'_a95 : display %9.3f sh_auto95_use[`i']
        sleep 2000
    }

    texdoc local 801_descr "Bowling alleys, billiard and pool parlors"
    sleep 2000
    texdoc local 102_descr "Canned and preserved fruits and vegetables"
    sleep 2000
    texdoc local 111_descr "Bakery products"
    sleep 2000
    texdoc local 100_descr "Meat products"
    sleep 2000
    texdoc local 110_descr "Grain mill products"
    sleep 2000
    texdoc local 112_descr "Sugar and confectionary products"
    sleep 2000
    texdoc local 101_descr "Dairy products"

    texdoc local notes "This table lists the sectors with the highest and lowest share of auto95 patents out of all patents in machinery in 1980-1998. The industry codes and descriptions of the sectors correspond to the ind6090 industries described in ALM."
    texdoc stlog close
    include ${code_dir}/config/texdoctools.do
    tex `texpreamble'
    /*tex
    \begin{tabular}{llccclc}
    \toprule
    \multicolumn{3}{@{}c}{Sectors with highest share of automated patents} & &  \multicolumn{3}{@{}c}{Sectors with lowest share of automated patents} \\ \noalign{\smallskip}  \multicolumn{2}{@{}l}{Industry} & Auto95 && \multicolumn{2}{l}{Industry} & Auto95 \\
    \cmidrule(r){1-3} \cmidrule(r){5-7} 
    `h1_id'   &  `756_descr' & `h1_a95' & & `l1_id'  &  `801_descr' & `l1_a95' \\
    `h2_id'   &  `206_descr' & `h2_a95' & & `l2_id'  &  `100_descr' & `l2_a95' \\
    `h3_id'   &  `470_descr' & `h3_a95' & & `l3_id'  &  `101_descr' & `l3_a95' \\
    `h4_id'   &  `271_descr' & `h4_a95' & & `l4_id'  &  `102_descr' & `l4_a95' \\
    `h5_id'   &  `130_descr' & `h5_a95' & & `l5_id'  &  `110_descr' & `l5_a95' \\
    `h6_id'   &  `212_descr' & `h6_a95' & & `l6_id'  &  `111_descr' & `l6_a95' \\
    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl
